PowerShell in Practice by Thomas Blake

PowerShell in Practice by Thomas Blake
Author: Thomas Blake
Language: eng
Format: epub
Tags: Manuscript Template, Public
Publisher: Thomas Blake

# Using ForEach-Object Cmdlet with Pipeline

$names | ForEach-Object {

Write-Output "Greetings, $_!"

}


6.3.4 Using Continue and Break

These statements control the flow within loops:

Continue: Skips the current iteration and moves to the next one.

Break: Exits the loop entirely.

Example:

for ($i = 1; $i -le 10; $i++) {

if ($i -eq 4) { continue } # Skip when $i is 4

if ($i -eq 8) { break } # Exit loop when $i is 8

Write-Output $i

}


6.3.5 Nested Loops

Loops within loops offer greater granularity and control but also require careful management to avoid complexity.

Example:

for ($i = 1; $i -le 3; $i++) {

for ($j = 1; $j -le 3; $j++) {

Write-Output "Outer loop iteration: $i, Inner loop iteration: $j"

}

}